package com.kantboot.business.water.repository;

import com.kantboot.business.water.domain.entity.BusWaterGoods;
import jakarta.transaction.Transactional;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface BusWaterGoodsRepository extends JpaRepository<BusWaterGoods,Long> {

    @Query("""
        from BusWaterGoods
        where (:#{#param.name} is null or :#{#param.name} = '' or name like concat('%',:#{#param.name},'%'))
        and (:#{#param.description} is null or :#{#param.description} = '' or description like concat('%',:#{#param.description},'%'))
        and (:#{#param.statusCode} is null or :#{#param.statusCode} = '' or statusCode = :#{#param.statusCode})
        and (:#{#param.isShowInVip} is null or isShowInVip = :#{#param.isShowInVip})
        and (:#{#param.isShowInUser} is null or isShowInUser = :#{#param.isShowInUser})
        AND  isDeleted = false 
        order by id desc
        """)
    Page<BusWaterGoods> getBodyData(@Param("param") BusWaterGoods goods, Pageable pageable);

    /**
     * 添加销量
     */
    @Transactional
    @Modifying
    @Query("""
        update BusWaterGoods set saleVolume = saleVolume + :salesVolume where id = :id
        """)
    void addSalesVolume(@Param("id") Long id, @Param("salesVolume") Long salesVolume);

}
